//
//  ICEmployeeSearchController.m
//  ICome
//
//  Created by zhangrongwu on 16/9/20.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICEmployeeSearchController.h"
#import "ICContactListCell.h"
#import "ICContactItem.h"
#import "ICEmpInfoModel.h"
#import "NSMutableAttributedString+Extension.h"
#import "UIImage+AvatarGeneration.h"

@interface ICEmployeeSearchController()

@property (nonatomic, strong)UILabel *emptyStyleLable;
@property (nonatomic, strong)NSString *searchText;
@property (nonatomic, strong)NSMutableArray *dataList;

@end

@implementation ICEmployeeSearchController
#pragma mark - LifeCycle
- (void) viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BACKGROUNDCOLOR;
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:animated];
    [self setUI];
}


- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

#pragma mark - UITableViewDataSource
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}

- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"";
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"ICContactListCell";
    ICContactListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[ICContactListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.leftFreeSpace = 0;
    
    ICEmpInfoModel *model = [self.dataList objectAtIndex:indexPath.row];
    if (model.isOrg) {
        NSString *searchTitle = [NSString stringWithFormat:@"%@   (%ld)", model.oName, model.count];
        cell.titleLabel.attributedText = [NSMutableAttributedString searchTtitle:searchTitle key:self.searchText keyColor:SEARCH_HEIGHT_COLOR];
        NSString *URL = [ICTools getMinImageURL:(model.photoId)];
        [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:URL] placeholderImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_organization"]];
    } else {
        NSString *searchTitle = [NSString stringWithFormat:@"%@   (%@)", model.eName, model.jobTitle];
        cell.titleLabel.attributedText = [NSMutableAttributedString searchTtitle:searchTitle key:self.searchText keyColor:SEARCH_HEIGHT_COLOR];
        NSString *URL = [ICTools getMinImageURL:(model.photoId)];
          [UIImage addAvatariamgeView:cell.headImageView withUrlStr:URL witheId:model.eId withName:model.eName];
//        [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:URL] placeholderImage:[UIImage imageNamed:@"App_personal_headimg"]];
    }
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55.0f * Level;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self emp_didSelectRowAtIndexPatch:indexPath];
}

#pragma mark - UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    self.searchText = searchController.searchBar.text;
    [self.dataList removeAllObjects];
    [self searchDataBySearchString:self.searchText];
    [self.tableView reloadData];
}

#pragma mark - event response

#pragma mark - private methods
- (void)setUI {
    self.tableView.frame = CGRectMake(0, 64, App_Frame_Width, APP_Frame_Height - 64 - 64);
}
// 搜索
- (void)searchDataBySearchString:(NSString *)String {
    // 数据库中查询数据，显示
    if (![ICTools stringEmpty:String]) {
        // 搜索数据
        [self getDataWithText:String];
    } else {
        // 恢复原样
    }
}

- (void)getDataWithText:(NSString *)text {
    NSDictionary *param = @{@"key":text,
                            @"type":@"1"};
    [[ICSocketRequestManager sharedInstance] searchEmpOROrgWithObject:param Success:^(id object) {
        [self reloadTableViewWithData:object];
    } Failure:^(NSError *error, id other) {
        [MBProgressHUD showError:error.domain];
    }];
}

- (void)reloadTableViewWithData:(id)response {
    NSArray *list = [response objectForKey:@"data"];
    if (list.count > 0) {
        self.emptyStyleLable.hidden = YES;
    } else {
        self.emptyStyleLable.hidden = NO;
    }
    
    for (NSDictionary *orgdict in list) {
        ICEmpInfoModel *model = [[ICEmpInfoModel alloc] initWithDict:orgdict];
        [self.dataList addObject:model];
    }
    [self.tableView reloadData];
}

- (void)emp_didSelectRowAtIndexPatch:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(didSelectRowAtIndexPath:DataSource:)]) {
        [self.delegate didSelectRowAtIndexPath:indexPath DataSource:self.dataList];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - getters and setters
- (NSMutableArray *)dataList {
    if (!_dataList) {
        _dataList = [[NSMutableArray alloc] init];
    }
    return _dataList;
}

- (UILabel *)emptyStyleLable {
    if (!_emptyStyleLable) {
        _emptyStyleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, App_Frame_Width, 30)];
        _emptyStyleLable.font  = [UIFont systemFontOfSize:17];
        _emptyStyleLable.backgroundColor = [UIColor clearColor];
        _emptyStyleLable.textAlignment = NSTextAlignmentCenter;
        _emptyStyleLable.textColor = ICRGB(0x9a9a9a);
        _emptyStyleLable.text = @"暂无搜索结果";
        _emptyStyleLable.hidden = YES;
        [self.view addSubview:_emptyStyleLable];
    }
    return _emptyStyleLable;
}
@end
